package User;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LlbraryDB {
    private String url = "jdbc:mysql://localhost:3306/bookmanagement";
    private String user = "root";
    private String password = "root";

    public void borrowBook(String borrowName, String borrowBook, LocalDateTime borrowTime) {
        String sql = "INSERT INTO borrowrecord(borrowname, borrowbook, borrowtime) VALUES (?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(url, user, password);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, borrowName);
            pstmt.setString(2, borrowBook);
            pstmt.setString(3, borrowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

            int rowsInserted = pstmt.executeUpdate();
            if (rowsInserted > 0) {
                System.out.println("A new borrow record was inserted successfully!");
                System.out.println(borrowName + " borrowed successfully!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
